package com.example.jetpack_viewmodel_livedata.sticky;

import androidx.lifecycle.MutableLiveData;

import java.util.HashMap;
import java.util.Map;

public class LiveDataBus {
    // 存放订阅者
    private Map<String, MutableLiveData<Object>> bus;
    private static LiveDataBus LiveDataBus = new LiveDataBus();

    private LiveDataBus() {
        bus = new HashMap<>();
    }

    public static LiveDataBus getInstance() {
        return LiveDataBus;
    }

    // 注册订阅者，（存入map）
    public synchronized <T> MutableLiveData<T> with(String key, Class<T> type, boolean sticky) {
        if (!bus.containsKey(key)) {
            if (sticky) {
                bus.put(key, new MutableLiveData<Object>());
            } else {
                bus.put(key, new NonStickyMutableLiveData<Object>());
            }
        }
        return (MutableLiveData<T>) bus.get(key);

    }
}

